<#assign domain="${ctx.contextPath}" />
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="x-ua-compatible" content="IE=Edge, chrome=1"/>
	<meta name="renderer" content="webkit">
	<title>authority</title>
	<link rel="stylesheet" href="${domain}/static/css/common/bootstrap.min.css">
	<link rel="stylesheet" href="${domain}/static/css/common/base.css">
	<link rel="stylesheet" href="${domain}/static/css/common/header.css">
	<link rel="stylesheet" href="${domain}/static/css/authority.css">
	<script src="${domain}/static/js/jquery-3.2.1.min.js"></script>
	<script src="${domain}/static/js/bootstrap.min.js"></script>
</head>
<body>  	
<div class="content">
	<!--订单管理内容-->
	<div class="authority">
		<div class="wrap">
			<div class="add-btn">添加员工</div>
			<div class="table-responsive">
				<table class="table table-striped">
					<thead>
					<tr>
						<th>姓名</th>
						<th>所属公司</th>
						<th>手机号</th>
						<th>角色</th>
						<th>操作</th>
					</tr>
					</thead>
					<tbody>
					<#if pm??&&pm?size gt 0>
					  <#list pm.list as user>  
						<tr>
							<td style="display: none;">${user.id}</td>
							<td>${user.name}</td>
							<td>${shop.name}</td>
							<td>${user.phone}</td>
							<td>${user.roleName}</td>
							<td>  
							    <#if  user.type !=0 >
						           <button id="edit" class="btn btn-default b-blue" style="margin-right: 10px;" onclick="getUserById('${user.id}')">编辑</button>
						           <button class="btn btn-default b-danger" name="deleteButton" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">删除</button>
							</td>
						        </#if>  
						</tr>
						</#list>
						</#if> 
					</tbody>
				</table>
				<div class="paging">
				共${pm.sumCount }条数据&nbsp;&nbsp;第${pm.currentPage }/${pm.sumPage }页&nbsp;&nbsp;
					<#if pm.currentPage<=1>
					  <a onclick="window.location.href='${domain}/permission/authority?shopId=${user.shopId}&pageNo=1';">
					  	上一页
					  </a>
					<#else> 
					    <a onclick="window.location.href='${domain}/permission/authority?shopId=${user.shopId}&pageNo=${pm.currentPage - 1}';">
				                    上一页
				        </a>
					</#if>
				&nbsp;&nbsp;
					<#if pm.currentPage = pm.sumPage>
						<a onclick="window.location.href='${domain}/permission/authority?shopId=${user.shopId}&pageNo=${pm.sumPage}';">
							下一页
						</a>
					<#else> 
					    <a onclick="window.location.href='${domain}/permission/authority?shopId=${user.shopId}&pageNo=${pm.currentPage+1}';">
							下一页
						</a>
					</#if>
			</div>
			</div>
		</div>
		<!--新增员工表格-->
		<div id="content1" class="main" style="display: none">
			<form class="from">
				<p class="close">关闭</p>
				<div>
					<h1>添加员工</h1>
					<div class="form-group">
						<input  type="text" class="form-control" id="name123" placeholder="姓名">
						<span id="spanName"></span>
					</div>
					<div class="form-group">
						<input type="text" class="form-control" id="phone" placeholder="手机号">
						<span id="spanPhone"></span>
					</div>
					<div class="form-group">
						<input type="text" class="form-control" id="roleName" placeholder="角色">
						<span id="spanRoleName"></span>
					</div>
					<div class="checkbox">
						<div class="line" id="serviceDiv">
						</div>
						<div class="line" id="menuDiv">
						</div>
					</div>
					<div id="look1" class="look">提交</div>
				</div>
			</form>
		</div>
		<!--编辑员工表格-->
		<div id="content2" class="main" style="display: none">
			<p class="close">关闭</p>
			<form class="from">
				<div>
					<h1>编辑员工<span class="close">关闭</span></h1>
					<div class="form-group">
						<input  type="text" class="form-control" id="name2" placeholder="姓名" readonly="true">
					</div>
					<div class="form-group">
						<input type="text" class="form-control" id="phone2" placeholder="手机号">
						<span id="spanPhone2"></span>
					</div>
					<div class="form-group">
						<input type="text" class="form-control" id="roleName2" placeholder="角色">
						<span id="spanRoleName2"></span>
					</div>
					<div class="checkbox">
						<div class="line"   id="myServiceDiv">
						  <!-
								<label class="checkbox-inline">
									<input type="checkbox" id="inlineCheckbox11" value="option3">反欺诈
								</label>
								<span style="visibility: hidden">11111111111111</span>
								<label class="checkbox-inline">
									<input type="checkbox" id="inlineCheckbox22" value="option3">评分卡
								</label>
							-->
						</div>
						<div class="line"  id="myMenuDiv">
						</div>
					</div>
					<input type="hidden" id="userId" value="">  
					<div id="look2" class="look">提交</div>
				</div>
			
			</form>
		</div>
	</div>
</div>
<!--
	作者：offline
	时间：2017-10-23
	描述：弹出框
-->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">提示信息</h4>
      </div>
      <div class="modal-body">
        <p>是否删除权限</p>
      </div>
      <div class="modal-footer">
      	<button type="button" class="btn btn-primary" id="deleteOrder">确定</button>
        <button type="button" class="btn btn-default" data-dismiss="modal" id="close">取消</button>
      </div>
    </div>
  </div>
</div>
</body>
<script>
var domain ='${domain}';
$('#exampleModal').on('hidden.bs.modal', function (e) {
  console.log('返回');
});

$("button[name='deleteButton']").on("click",function(){
	console.log("弹窗");
	var authorityId  = $(this).parent().parent().children()[0].innerText;
	$("#deleteOrder").on("click",function(){
		console.log("删除权限： "+authorityId);
		//删除方法
        deleteUser(authorityId);
	});
});

	$(".close").on('click',function() {
		$(".main").hide();
	})
	
	$(function() {
		$(".add-btn").on('click',function() {
		   getUserPermission('');
		});
		$("#look1").on('click',function() {
			addUser();
		});
		
		$("#look2").on('click',function() {
			editUser();
			$("#content1").hide();
		})
	})
	
	function loginout(){
		var url = domain+'/loginout';
		window.location.href = url;
	}
	
     function addUser(){ 
		   var name = $("#name123").val(); 
		   var phone = $("#phone").val(); 
		   var roleName = $("#roleName").val(); 
		   
		   name  = $.trim($("#name123").val());
			var reg1=/^\d+$/;
			if(name==''||name==null||name==undefined){
				$("#spanName").html("姓名不能为空！");
				$("#spanName").css({"color":"#F00"});
				return false;
			}else{
				$("#spanName").hide();
			}
		   
		   phone  = $.trim($("#phone").val());
		   var reg = /^1\d{10}$/;
		   if(phone==''||phone==null||phone==undefined){
		     	$("#spanPhone").html("手机号不能为空！");
				$("#spanPhone").css({"color":"#F00"});
				return false;
		   }else if(!reg.test(phone)){
		       $("#spanPhone").html("手机号输入格式错误 ， 请重新输入！");
			   $("#spanPhone").css({"color":"#F00"});
		       return false;
		   }else{
				$("#spanPhone").hide();
			}
			
		   roleName  = $.trim($("#roleName").val());
		   if(roleName==''||roleName==null||roleName==undefined){
		   	   $("#spanRoleName").html("角色不能为空！");
			   $("#spanRoleName").css({"color":"#F00"});
		       return false;
		   }else{
				$("#spanRoleName").hide();
			}
		   
		   var chk_menu ='';
		   $('input[name="menuCheckBox"]:checked').each(function(){
			    chk_menu += $(this).val()+",";
		   }); 
		   
		   var chk_service ='';
		   $('input[name="serviceCheckBox"]:checked').each(function(){
			      chk_service += $(this).val()+",";
		   }); 
		   
		   var jsonData = {
		  		 "name":name,
		         "phone":phone,
		         "roleName":roleName,
		         "menuIds":chk_menu,
		         "serviceIds":chk_service
		   };
		   
		   $.ajax({
					type : 'POST',
					url : domain+'/permission/addUser',
					data : jsonData,
					dataType : 'json',
					success : function(data) {
					   
						
				       if(data.success){
				            $("#content1").hide();
						location.href = domain+'/permission/authority'; 
				       }else{
				           alert(data.msg);
				       }
					}
			});
			
		}
     
		function getUserById(id){
		   
		   $("#content2").show();
		   
		   var jsonData = {
		         "id":id
		   };
		   
		   $.ajax({
					type : 'POST',
					url : domain+'/permission/findById',
					data : jsonData,
					dataType : 'json',
					success : function(data) {
						$("#name2").val(data.user.name);
						$("#phone2").val(data.user.phone);
						$("#roleName2").val(data.user.roleName);
						$("#userId").val(id);
					}
			});

			getUserPermission(id);
		}
		
		function editUser(){
		   var name2 = $("#name2").val(); 
		   var phone2 = $("#phone2").val(); 
		   var roleName2 = $("#roleName2").val(); 
		   
		   phone2  = $.trim($("#phone2").val());
		   var reg = /^1\d{10}$/;
		   if(phone2==''||phone2==null||phone2==undefined){
		     	$("#spanPhone2").html("手机号不能为空！");
				$("#spanPhone2").css({"color":"#F00"});
				return false;
		   }else if(!reg.test(phone2)){
		       $("#spanPhone2").html("手机号输入格式错误 ， 请重新输入！");
			   $("#spanPhone2").css({"color":"#F00"});
		       return false;
		   }else{
				$("#spanPhone2").hide();
			}

		   roleName2  = $.trim($("#roleName2").val());
		   if(roleName2==''||roleName2==null||roleName2==undefined){
		   	   $("#spanRoleName2").html("角色不能为空！");
			   $("#spanRoleName2").css({"color":"#F00"});
		       return false;
		   }else{
				$("#spanRoleName2").hide();
			}
			
		   var chk_menu ='';
		   $('input[name="myMenuCheckBox"]:checked').each(function(){
			    chk_menu += $(this).val()+",";
		   }); 
		   
		   var chk_service ='';
		   $('input[name="myServiceCheckBox"]:checked').each(function(){
			      chk_service += $(this).val()+",";
		   }); 
		   
		   var jsonData = {
		  		 "name":name2,
		         "phone":phone2,
		         "roleName":roleName2,
		         "id":$("#userId").val(),
		         "menuIds":chk_menu,
		         "serviceIds":chk_service
		   };
		   $.ajax({
					type : 'POST',
					url : domain+'/permission/editUser',
					data : jsonData,
					dataType : 'json',
					success : function(data) {
					    $("#content1").hide();
					    $("#content2").hide();
					    location.href = domain+'/permission/authority'; 
					} 
			});
		}
		
		function deleteUser(id){
			var jsonData = {
		         "id":id
		   };
		   $.ajax({
					type : 'POST',
					url : domain+'/permission/deleteUser',
					data : jsonData,
					dataType : 'json',
					success : function(data) {
						location.href = domain+'/permission/authority'; 
					} 
			});
		}
		
	  function getUserPermission(id){
		   if(id!=''){
		       $("#content2").show();
		   }else{
		      $("#content1").show();
		   }
		   $.ajax({
					type : 'POST',
					url : domain+'/permission/getUserPermission',
					data : {"id":id},
					dataType : 'json',
					success : function(data) {
						 var serviceList  =  data.servicePermissionList;
						 if(serviceList!=null){
						        var serviceView  ="";
						        //初始化服务
						        for(var i =0;i<serviceList.length;i++){
						              var pId =  serviceList[i].permissionId;
						              
						              //修改时传入的用户id
						              if(id!=''){
						                     var flag =  0;
						                      var myServiceList  =  data.myServicePermissionList;
						                      if(myServiceList!=null){
						                              for(var j =0;j<myServiceList.length;j++){
						                                  if(myServiceList[j].permissionId == pId){
						                                      flag = 1;
						                                      break;
						                                  }
						                             }
						                      }
						                    if(pId ==1 ){
						                        if(flag ==1){
						                            serviceView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myServiceCheckBox'   value='"+pId+"'   checked ='true'>&nbsp;&nbsp;&nbsp;反欺诈</label>" ;
						                        }else{
						                            serviceView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myServiceCheckBox'   value='"+pId+"'   >&nbsp;&nbsp;&nbsp; 反欺诈</label>" ;
						                        }
							                }else{
							                    if(flag ==1){
						                            serviceView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myServiceCheckBox'   value='"+pId+"'   checked ='true'>&nbsp;&nbsp;&nbsp;评分卡</label>" ;
						                        }else{
						                            serviceView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myServiceCheckBox'   value='"+pId+"'   >&nbsp;&nbsp;&nbsp;评分卡</label>" ;
						                        }
							                }
						              }else{
						                    if(pId ==1 ){
							                   serviceView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='serviceCheckBox'   value='"+pId+"'   checked ='true'>&nbsp;&nbsp;&nbsp;反欺诈</label>" ;
							                }else{
							                  serviceView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='serviceCheckBox'   value='"+pId+"'  checked ='true' >&nbsp;&nbsp;&nbsp;评分卡</label>" ;
							                }
						              }						                
						        }
						         if(id!=''){
						           $("#myServiceDiv") .html(serviceView);
						         }else{
						            $("#serviceDiv") .html(serviceView);
						         }
						 }
						 var menuList  =  data.menuPermissionList;
						 if(menuList!=null){
						        var menuView  ="";
						        //初始化菜单
						        for(var i =0;i<menuList.length;i++){
						              var pId =  menuList[i].permissionId;
						              
						               //修改时传入的用户id
						              if(id!=''){
						                  var flag =  0;                  
						                  var myMenuList  =  data.myMenuPermissionList;
						                   if(myMenuList!=null){
						                              for(var j =0;j<myMenuList.length;j++){
						                                  if(myMenuList[j].permissionId == pId){
						                                      flag = 1;
						                                      break;
						                                  }
						                             }
						                      }
						                      if(pId ==1 ){
							                        if(flag ==1){
							                            menuView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myMenuCheckBox'   value='"+pId+"'   checked ='true'>权限管理</label>" ;
							                        }else{
							                            menuView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myMenuCheckBox'   value='"+pId+"'   >权限管理</label>" ;
							                        }
								                }else{
								                    if(flag ==1){
							                            menuView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myMenuCheckBox'   value='"+pId+"'   checked ='true'>订单管理</label>" ;
							                        }else{
							                            menuView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='myMenuCheckBox'   value='"+pId+"'   >订单管理</label>" ;
							                        }
								                }
							           }else{
							                 if(pId ==1 ){
						                          menuView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='menuCheckBox'     value='"+pId+"' checked ='true'> 权限管理</label>" ;
								              }else{
								                  menuView +=   "<label class='checkbox-inline'> <input type='checkbox'  name='menuCheckBox'   value='"+pId+"' checked ='true'>订单管理</label>" ;
								              }
							           }    
						        }
						         if(id!=''){
						             $("#myMenuDiv") .html(menuView);
						         }else{
						            $("#menuDiv") .html(menuView);
						         }
						 }
					} 
			});
		}
</script>
</html>
